package cn.lanqiao.system.controller;

import cn.lanqiao.system.entity.Products;
import cn.lanqiao.system.service.ProductsService;
import cn.lanqiao.system.util.Result;
import cn.lanqiao.system.vo.ProductsVo;
import cn.lanqiao.system.vo.QueryProductsVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Products)控制层
 *
 * @author david
 * @since 2024-04-09 19:14:21
 */
@Api(tags = "控制层")
@RestController
@RequestMapping("/products")
@CrossOrigin // 线上不需要 （要注掉）
public class ProductsController {
    /**
     * 服务对象
     */
    @Autowired
    private ProductsService productsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "获取单条数据")
    @GetMapping("/getProductsById/{id}")
    public Result getProductsById(@ApiParam(name = "id", value = "编号", required = true) @PathVariable Integer id) {
        Products products = productsService.getProductsById(id);
        return Result.success(products);
    }

    /**
     * 分页查询
     *
     * @param queryProductsVo
     * @return PageInfo<Products>
     */
    @ApiOperation(value = "获取分页数据")
    @PostMapping("/getProductsPage")
    public Result getProductsPage(@ApiParam(name = "queryProductsVo", value = "", required = true) @RequestBody QueryProductsVo queryProductsVo) {
        PageHelper.startPage(queryProductsVo.getPageNum(), queryProductsVo.getPageSize());
        List<ProductsVo> list = productsService.selectProductsList(queryProductsVo);
        return Result.success(new PageInfo<>(list));
    }

    /**
     * 导出查询
     *
     * @param queryProductsVo
     * @return Products
     */
    @ApiOperation(value = "获取导出数据")
    @PostMapping("/getProductsList")
    public Result getProductsList(@ApiParam(name = "queryProductsVo", value = "", required = true) @RequestBody QueryProductsVo queryProductsVo) {
        List<ProductsVo> list = productsService.selectProductsList(queryProductsVo);
        return Result.success(list);
    }

    /**
     * 新增或保存
     *
     * @param
     * @return
     */
    @ApiOperation(value = "修改或新增")
    @PostMapping("/updateOrSaveProducts")
    public Result updateOrSaveProducts(@ApiParam(name = "updateOrSaveProductsVo", value = "", required = true) @RequestBody ProductsVo updateOrSaveProductsVo) {
        productsService.insertOrUpdate(updateOrSaveProductsVo);
       return Result.success();
    }


    /**
     * 批量删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/deleteProductsById/{id}")
    public Result deleteProductsByIds(@ApiParam(name = "idList", value = "主键集合", required = true) @PathVariable String id) {
        productsService.deleteByIds(id);
        return Result.success();
    }

}
